package com.time.workshop.ui.fragment;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

import com.time.workshop.API;
import com.time.workshop.BaseFragment;
import com.time.workshop.R;
import com.time.workshop.bean.OrderType;
import com.time.workshop.bean.RequestOrder;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.utils.CommonUtils;
import com.time.workshop.utils.LaoBanDialog;
import com.time.workshop.utils.NewToast;
import com.time.workshop.utils.OnClickOKListener;
import com.time.workshop.view.TabHeadView;

public class BuyTimeFragment extends BaseFragment implements OnClickListener
{

	@V
	private TabHeadView bv_addorder_back;
	@V
	private EditText buytime_nameEditText, buytime_phoneEditText, buytime_addressEditText, buytime_remarksEditText;
	@V
	private Button btn_addorder_ok;
	@V
	private TextView buytime_timeTextView;

	private String remark, name, address, phone, buytime;

	private DatePicker datePicker;
	private TimePicker timePicker;
	private AlertDialog dialog;
	public Calendar time = Calendar.getInstance(Locale.CHINA);

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		return inflater.inflate(R.layout.activity_addorder, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState)
	{
		super.onViewCreated(view, savedInstanceState);
		Injector.getInstance().inject(getActivity(), this, view);
		init();
	}

	private void init()
	{
		bv_addorder_back.getLeftButton().setBackgroundResource(R.drawable.btn_back1_normal);
		bv_addorder_back.getRightButton().setVisibility(View.GONE);
		bv_addorder_back.getLeftButton().setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
				mSuperActivity.menu.showMenu();
			}
		});
		btn_addorder_ok.setOnClickListener(this);
		buytime_timeTextView.setOnClickListener(this);
	}

	@Override
	public void onClick(View view)
	{
		switch (view.getId())
		{
		case R.id.btn_addorder_ok:
			submit();
			break;
		case R.id.buytime_timeTextView:
			dateTimePickerDialog();
			break;

		default:
			break;
		}
	}

	private void submit()
	{
		buytime = buytime_timeTextView.getText().toString().trim();
		phone = buytime_phoneEditText.getText().toString().trim();
		name = buytime_nameEditText.getText().toString().trim();
		address = buytime_addressEditText.getText().toString().trim();
		remark = buytime_remarksEditText.getText().toString().trim();
		if ("".equals(CommonUtils.isNull(buytime)))
		{
			NewToast.makeText(getActivity(), getResources().getString(R.string.toast_run_time));
			return;
		}
		if ("".equals(CommonUtils.isNull(name)))
		{
			NewToast.makeText(getActivity(), getResources().getString(R.string.toast_name));
			return;
		}
		if (!CommonUtils.isMobile(phone))
		{
			NewToast.makeText(getActivity(), getResources().getString(R.string.toast_phone));
			return;
		}
		if ("".equals(CommonUtils.isNull(address)))
		{
			NewToast.makeText(getActivity(), getResources().getString(R.string.toast_address));
			return;
		}
		showConfirmDialog();
	}

	private LaoBanDialog dlg;

	/**
	 * 显示 Dailog
	 */
	private void showConfirmDialog()
	{

		dlg = new LaoBanDialog(getActivity());
		if (dlg.isShowing())
		{
			dlg.dismiss();
			return;
		}
		dlg.onCreate();
		dlg.setOnClickOK(new OnClickOKListener()
		{

			@Override
			public void setOnClickOK()
			{
				dlg.dismiss();
				RequestOrder requestOrder = new RequestOrder();
				requestOrder.ORDER_TYPE = OrderType.SJGF.toString();
				requestOrder.reservation_time = buytime;
				requestOrder.contactor_person = name;
				requestOrder.contactor_phone = phone;
				requestOrder.contactor_address = address;
				requestOrder.remark = remark;
				API.submitOrder(requestOrder, mSuperActivity);
			}
		});
	}

	// 选择时间
	private AlertDialog dateTimePickerDialog()
	{
		LinearLayout dateTimeLayout = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.date_time_picker, null);
		datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.DatePicker);
		timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.TimePicker);
		timePicker.setIs24HourView(true);

		OnTimeChangedListener timeListener = new OnTimeChangedListener()
		{
			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
			{
				time.set(Calendar.HOUR_OF_DAY, hourOfDay);
				time.set(Calendar.MINUTE, minute);

			}
		};

		timePicker.setOnTimeChangedListener(timeListener);
		OnDateChangedListener dateListener = new OnDateChangedListener()
		{

			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
			{
				time.set(Calendar.YEAR, year);
				time.set(Calendar.MONTH, monthOfYear);
				time.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			}
		};

		datePicker.init(time.get(Calendar.YEAR), time.get(Calendar.MONTH), time.get(Calendar.DAY_OF_MONTH), dateListener);
		timePicker.setCurrentHour(time.get(Calendar.HOUR_OF_DAY));
		timePicker.setCurrentMinute(time.get(Calendar.MINUTE));

		dialog = new AlertDialog.Builder(getActivity()).setTitle("设置日期时间").setView(dateTimeLayout).setPositiveButton("确定", new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{

				time.set(Calendar.YEAR, datePicker.getYear());
				time.set(Calendar.MONTH, datePicker.getMonth());
				time.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
				time.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
				time.set(Calendar.MINUTE, timePicker.getCurrentMinute());
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				buytime_timeTextView.setText(format.format(time.getTime()));
			}
		}).setNegativeButton("取消", new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{

			}
		}).show();
		return dialog;
	}

}
